package crl.lottery.controller
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	
	import flash.display.Loader;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import crl.lottery.event.LotteryEvent;
	import crl.lottery.model.Logic;
	import crl.lottery.model.LogicConfig;
	import crl.lottery.model.vo.ItemVO;
	import crl.lottery.model.vo.RotationVO;
	import crl.lottery.view.LotteryView;
	import crl.lottery.view.NoticeManager;
	
	/**
	 * 抽奖控制器; 
	 * @author crl;
	 * 
	 */	
	public class LotteryController extends EventDispatcher
	{
		private var _view:LotteryView;
		private var _model:Logic;
		
		private var currentSelectIndex:int;
		private var rotationvo:RotationVO;
		
		private var _config:LogicConfig;
		public function LotteryController()
		{
			this.view=new LotteryView();
			
			this.model=new Logic();
			rotationvo=new RotationVO();
		}
		
		
		
		/**
		 * 点击开始游戏时; 
		 * @param event
		 * 
		 */		
		private function startClickHandler(event:MouseEvent):void{
			
			this.enabel=false;
			
			var resultRequest:ResultRequest=new ResultRequest();
			resultRequest.url=LogicConfig.lottery_result_url;
			
			resultRequest.addEventListener(Event.COMPLETE,startCompletHandler);
			resultRequest.addEventListener(ErrorEvent.ERROR,startCompletHandler);
			
			
			resultRequest.send(null);
			
		}
		
		protected function startCompletHandler(event:Event):void
		{
			var resultRequest:ResultRequest=event.target as ResultRequest;
			resultRequest.removeEventListener(Event.COMPLETE,startCompletHandler);
			resultRequest.removeEventListener(ErrorEvent.ERROR,startCompletHandler);
			
			
			var message:String;
			if(event is ErrorEvent){
				message="提交数据错误:"+(event as ErrorEvent).text;
				NoticeManager.alert("提示",message);
				this.enabel=true;
				return;
			}
			
			
		
			currentSelectIndex=int(resultRequest.data)
			//currentSelectIndex=1;
			var resultRotation:int=360*2+currentSelectIndex*(model.du);
			
			rotationvo.rotation=view.rotaPane.rotation;
			if(rotationvo.rotation>=180){
				rotationvo.rotation-=360;
			}
			
			if(currentTween){
				TweenLite.killTweensOf(rotationvo,true);
			}
			
			currentTween=TweenLite.to(rotationvo, 5, {rotation:resultRotation, ease:Cubic.easeOut,onUpdate:onUpdate,onComplete:onComplete});
		}
		private var currentTween:TweenLite;
		
		private function onUpdate():void{
			var rot:int=rotationvo.rotation%360;
			if(rot>180){
				rot-=360;
			}
			view.rotaPane.rotation=rot;
		}
		
		/**
		 * 抽奖结果; 
		 * 
		 */		
		private function onComplete():void{
			
			var resultKey:int=currentSelectIndex;
			
			var itemvo:ItemVO=_config.getItemByKey(resultKey);
			trace(resultKey,itemvo.id);
			
			
			var resultEffect:Loader=this.view.resultEffect;
			try{
				resultEffect.close();
			}
			catch(e:Error){	
				
			}
			resultEffect.unload();
			
			
			if(itemvo.order<4){
				resultEffect.load(new URLRequest("assets/default.swf"));
			}else{
				resultEffect.load(new URLRequest("assets/best.swf"));
			}
			
			var message:String="您抽到了:"+itemvo.name;
			NoticeManager.lotteryAlert("抽奖结果提示",message,finishHandler);
			this.enabel=true;
			
			
			this.dispatchEvent(new LotteryEvent(LotteryEvent.RESULT,resultKey));
		}
		
		
		private function finishHandler(code:int):void{
			try{
				this.view.resultEffect.close();
			}catch(e:Error){
			}
			this.view.resultEffect.unload();
		}
		
		
		public function set enabel(bool:Boolean):void{
			if(bool){
				view.startBtn.mouseEnabled=true;
				view.startBtn.filters=view.emptyFilter;
			}else{
				view.startBtn.filters=view.disabeFilter;
				view.startBtn.mouseEnabled=view.startBtn.mouseChildren=false;
			}
		}
		
		public function set view(value:LotteryView):void{
			_view=value;
			_view.startBtn.addEventListener(MouseEvent.CLICK,startClickHandler);
		}
		
		public function get view():LotteryView{
			return this._view;
		}
		
		public function set model(value:Logic):void{
			_model=value;
		}
		public function get model():Logic{
			return this._model;
		}
		
		
		public function set config(value:LogicConfig):void{
			this._config=value;
			model.data=_config.getList();
		}
	}
}